{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 使用回溯法求解 0-1 背包问题\n",
    "深度优先搜索.\n",
    "算法步骤：\n",
    "1. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 基本设置\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "10 58 [20, 10, 9, 9, 17, 16, 9, 12, 9, 12] [19, 16, 23, 5, 19, 13, 8, 4, 14, 21]\n"
     ]
    }
   ],
   "source": [
    "# 数据读取\n",
    "import re\n",
    "def LoadData(path):\n",
    "    '''\n",
    "    数据读取。\n",
    "    Input:\n",
    "    - path: 数据路径.\n",
    "    \n",
    "    Return:\n",
    "    - cache: a list contains (n,c,w,v)\n",
    "    '''\n",
    "    cache = []\n",
    "    with open(path,'r') as f:  \n",
    "         for i in range(4):\n",
    "            x = re.findall(r'\\d+',f.readline())\n",
    "            x = [int(i) for i in x]\n",
    "            cache.append(x)\n",
    "    return cache\n",
    "    \n",
    "path = 'test2.txt'\n",
    "n,c,w,v = LoadData(path)\n",
    "n = n[0]\n",
    "c = c[0]\n",
    "print(n,c,w,v)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "93"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def Backtracking(n,w,v,state=c,stage=0):\n",
    "    '''\n",
    "    使用回溯法求解 0-1 背包问题.\n",
    "    Input:\n",
    "    - n: 物品数量.\n",
    "    - w: 物品重量.\n",
    "    - v: 物品价值.\n",
    "    - state: 剩余空间.\n",
    "    - stage: 处于哪个阶段.\n",
    "    \n",
    "    Return:\n",
    "    - value: 最优价值.\n",
    "    '''\n",
    "    if stage == n-1:\n",
    "        if w[stage]<= state:\n",
    "            return v[stage]\n",
    "        else:\n",
    "            return 0\n",
    "    if state < 0:\n",
    "        # 不可行\n",
    "        return None\n",
    "    # 装入该物品\n",
    "    value1 = Backtracking(n,w,v,state=state-w[stage],stage=stage+1)\n",
    "    # 不装入该物品\n",
    "    value2 = Backtracking(n,w,v,state=state,stage=stage+1)\n",
    "    # value2 不可能是 None\n",
    "    if value1 == None:\n",
    "        value = value2\n",
    "    elif value1 != None:\n",
    "        value = max(value1 + v[stage],value2)\n",
    "    return value\n",
    "Backtracking(n,w,v,state=c)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "torch",
   "language": "python",
   "name": "torch"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
